<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
					fileList: [],
					fileName: '',
					fileSaveName: '',
					files: [],
					fileKey: "37",
					dir: "",
				userListCache: "rps-cg-tmaterialPurchaseContractReview-userlist-cache",
				supplierListCache: "rps-cg-tmaterialPurchaseContractReview-supperlist-cache",
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName ', 'userId'])
		},
		methods: {
			onSelectContract(item){
				let _that = this
				_that.$refs.autoform.formdata.contractCode = item.contractCode
				_that.$refs.autoform.formdata.contractSupplierId = item.tsupplierId
				_that.$refs.autoform.formdata.contractSupplierName = item.tsupplierCode
				_that.$refs.autoform.formdata.contractSupplierShortName = item.tsupplierShortName
				_that.$refs.autoform.formdata.contractTemplateTypeCode = item.contractTemplateTypeCode
				_that.$refs.autoform.formdata.contractTemplateTypeName = item.contractTemplateTypeName
				_that.$refs.autoform.initSelectIndex("title1"   ,"contractTemplateTypeCode")
				_that.$refs.autoform.formdata.remark = item.remark
				_that.$refs.autoform.formdata.contractDeliverDate = item.contractDeliverDate
				
			},
			getList(userId){
				let _that = this
				_that.om.request(
					"materialPurchaseContractList/getListByUserId",
					{
						userId:userId
					}
				).then(res=>{
					if(!res.length){
						let editor = _that.$refs.autoform.getEditor("title1","contractCode")
						_that.om.toast(_that.$refs.autoform.formdata.contractUserName+"没有可修改的"+editor.title+"!")
						_that.$refs.autoform.changeEditor("title1","contractCode","type","textbox")
					}else{
						_that.$refs.autoform.changeEditor("title1","contractCode","type","selectbox")
						_that.$refs.autoform.changeEditor("title1","contractCode","data",configSelectData(res,"contractCode","contractCode"))
						_that.$refs.autoform.initSelectIndex("title1","contractCode")
						_that.$refs.autoform.changeEditor("title1","contractCode","onSelect","onSelectContract")
					}
				})
			},
			formSubmit() {
				let _that = this
				if(_that.formdata.contractDeliverDate === null){
					_that.formdata.contractDeliverDate = ""
				}
				_that.om.formSave("tmaterialPurchaseContractReview/addTmaterialPurchaseContractReviewByH5", _that.formdata, _that)
			},
			getDir() {
				let _that = this
				_that.om.request(
					"uploadFiles/getDir", {
						fileKey: _that.fileKey, //
						fileName: _that.fileUrl //保存后文件名
					}
				).then(res => {
					_that.formdata.dir = res.value
				})
			},
			verifySupplier() {
				let _that = this
				if (!_that.$refs.autoform.formdata.contractTypeCode) {
					let editor = _that.$refs.autoform.getEditor("title1", "contractTypeCode")
					_that.om.toast("请先选择" + editor.title, 3000)
					return false
				} else {
					return true
				}
			},
			verifyUser() {
				let _that = this
				if (!_that.$refs.autoform.formdata.contractTypeCode) {
					let editor = _that.$refs.autoform.getEditor("title1", "contractTypeCode")
					_that.om.toast("请先选择" + editor.title, 3000)
					return false
				} else {
					return true
				}
			},
			initContractTemplateType() {
				let _that = this
				_that.om.request(
					urlUtil.dictZeroComboboxUrl("37970").url,
					urlUtil.dictZeroComboboxUrl("37970").param
				).then(res => {
					_that.$refs.autoform.changeEditor("title1", "contractTemplateTypeCode", "data", configSelectData(res, "code",
						"dicname"))
					_that.$refs.autoform.initSelectIndex("title1", "contractTemplateTypeCode")
				})
			},
			initPosList(userId) {
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/findPosByUserIdAndInstId", {
						userid: userId
					}
				).then(res => {
					_that.$refs.autoform.changeEditor("title1", "contractPosId", "data", configSelectData(res, "id", "posname"))
					_that.$refs.autoform.initSelectIndex("title1", "contractPosId")
				})
			},
			initUserList() {
				let _that = this
				_that.$refs.autoform.changeEditor("title1", "contractUserId", {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({}),
						namefield: "username",
						url: 'orgInstPosUserOfficial/findSameInstUserListByUserId',
						cachename: _that.userListCache,
						initload: true,
						searchfield: "q",
						placeholder: "请输入姓名"
					}
				})
			},
			initContractSupplier() {
				let _that = this
				_that.$refs.autoform.changeEditor("title1", "contractSupplierId", {
					link: "../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({}),
						namefield: "supplierCode",
						extrafield: "supplierShortName",
						searchfield: "q",
						url: 'tsupplier/likeSerchByOrg',
						cachename: _that.supplierListCache,
						initload: true,
						placeholder: "请输入公司名称或供方代码"
					}
				})
			},
			onSelectContractType(item) {
				let _that = this
				if(_that.$refs.autoform.formdata.contractUserId && item.code==="37469"){
					_that.getList(_that.$refs.autoform.formdata.contractUserId)
				}
				_that.initUserList()
				_that.initContractSupplier()
				_that.initContractTemplateType()
			},
			initLoad(options) {
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url: 'common/findCategoryByBusinessCode',
						param: {
							businessCode: options.code
						}
					},
					{
						url: "dictZero/getListByCode",
						param: {
							code: "37468",
							inCode: "37470,37469"
						},
						method: "post"
					}
				]
				if (fileId) {
					ajaxArr.push({
						url: "tmaterialPurchaseContractReview/getInfo",
						param: {
							id: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], 'id', 'posname'), mainData)
					changeEditor("topinfo", "cateGoryId", "data", configSelectData(res[1].value, 'id', 'dicname'), mainData)
					changeEditor("title1", "contractTypeCode", "data", configSelectData(res[2], "code", "dicname"), mainData)
					if (_that.subConfigData["title4"]) {
						let configData = _that.subConfigData["title4"]
						let attr = {}
						if (hasEditor('title4', 'userId', configData)) {
							attr.titleTemplate = '姓名:<%= userName %>'
						}
						if (hasEditor('title4', 'posId', configData)) {
							attr.subtitle1Template = '岗位:<%= posName %>'
						}
						changeEditor('title4', 'objStr', attr, mainData);
					}
					changeEditor("title5", "fileUpload", {
						link: "../../public/uploadFile/uploadFile",
						"linkParam": {
							url: "/tmaterialPurchaseContractReview/uploadFile.do",
							type: "one-on-one",
						}
					}, null, mainData)
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[3].value.tmaterialPurchaseContractReview) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[3].value.tmaterialPurchaseContractReview[
									index])
								_that.$set(_that.formdata, index, res[3].value.tmaterialPurchaseContractReview[index])
							}
						}
						let userList = _that._.cloneDeep(res[3].value.tmaterialPurchaseContractReview_users)
						_that.subEditorObj["title4"].workList = userList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "objStr", JSON.stringify(userList))
						_that.$set(_that.formdata, "objStr", JSON.stringify(userList))
						delete res[3].value.tmaterialPurchaseContractReview_users.userList
						
						_that.$refs.autoform.formdata.fileUpload = res[3].value.fileTable
						_that.$refs.autoform.formdata.fileName = res[3].value.fileTable.fileName
						_that.formdata.fileName = res[3].value.fileTable.fileName
						_that.formdata.fileSaveName = res[3].value.fileTable.fileSaveName
						_that.formdata.fileUrl = res[3].value.fileTable.fileSaveName
						_that.getDir() //获取保存文件夹
						
						_that.$nextTick(function() {
							_that.$refs.autoform.initSelectIndex("topinfo", "zposId")
							_that.$refs.autoform.initSelectIndex("title1", "contractTypeCode")
							_that.initPosList(_that.$refs.autoform.formdata.contractUserId)
							_that.initContractTemplateType()
							_that.initContractSupplier()
							_that.initUserList()
							if(_that.$refs.autoform.formdata.contractUserId && _that.$refs.autoform.formdata.contractTypeCode==="37469"){
								_that.getList(_that.$refs.autoform.formdata.contractUserId)
							}
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				if (_that.files && _that.files.length > 1 && _that.edit !== "true") {
					_that.$refs.autoform.formdata.fileName = _that.files.length + ("个文件")
					_that.$refs.autoform.formdata.fileList = _that.files
				} else if (_that.edit !== true) {
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileName",_that.fileName)
					_that.$set(_that.formdata,"fileName",_that.fileName)
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileList",_that.files)
					_that.$set(_that.formdata,"fileList",_that.files)
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileUpload", _that.files)
					_that.$set(_that.formdata,"fileUpload", _that.files)
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileUrl", _that.fileSaveName)
					_that.$set(_that.formdata,"fileUrl", _that.fileSaveName)
					_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"fileSaveName",_that.fileSaveName)
					_that.$set(_that.formdata,"fileSaveName", _that.fileSaveName)
				}
				for (let fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key: _that.userListCache,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.contractUserId = d.id
							if(formdata.contractTypeCode === "37469"){
								_that.getList(d.id)
							}else{
								_that.$refs.autoform.changeEditor("title1","contractCode","type","textbox")
							}
							formdata.contractUserName = d.username
							formdata.contractPosId = d.salaryPosId
							formdata.contractPosName = d.salaryPosName
							_that.initPosList(d.id)
							uni.removeStorage({
								key: _that.userListCache
							})
						}
					}
				})
				uni.getStorage({
					key: _that.supplierListCache,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.contractSupplierId = d.id
							formdata.contractSupplierName = d.supplierCode
							formdata.contractSupplierShortName = d.supplierShortName
							uni.removeStorage({
								key: _that.supplierListCache
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>
